package helpers
{
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.data.LoaderMaxVars;
	import com.greensock.loading.data.SWFLoaderVars;
	import com.greensock.loading.data.XMLLoaderVars;
	import com.greensock.loading.DataLoader;
	import com.greensock.loading.display.ContentDisplay;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.SWFLoader;
	import com.greensock.loading.XMLLoader;
	import com.greensock.TweenLite;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.Stage3D;
	import flash.display3D.Context3D;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.utils.ByteArray;
	import flash.utils.setInterval;
	import objects.LoaderComponent;
	
	/**
	 * ...
	 * @author drowller
	 */
	
	public class LoaderManager
	{
		private var queue:XMLLoader;
		private var queue1:LoaderMax;
		public var containerSWF:Sprite = new Sprite();
		
		public var loaders:Array = [];
		public var swfs:Array = [];
		
		private var loaderCount:int = 0;
		private var loaderNum:int = 0;
		
		private var currentLoader:DataLoader;
		private var lastLoader:DataLoader;
		private var tempContainer:ContentDisplay;
		
		private var ldr2:Loader = new Loader();
		
		public function LoaderManager()
		{
			
			LoaderMax.activate([SWFLoader, XMLLoader, DataLoader]);
			//create a LoaderMax named "mainQueue" and set up onProgress, onComplete and onError listeners
			//var queue:LoaderMax = new LoaderMax({name: "mainQueue", onProgress: progressHandler, onComplete: completeHandler, onError: errorHandler});
			//queue.append(new SWFLoader("Trening.swf", {name: "childClip", estimatedBytes: 3000, container: containerSWF, x: 0, autoPlay: false}));
			var varsMax:LoaderMaxVars = new LoaderMaxVars();
			varsMax.onComplete(completeHandler);
			//varsMax.onProgress(progressHandler);
			varsMax.loaders(loaders);
			//queue = new XMLLoader("data.xml", {onComplete: completeHandler, estimatedBytes: 50000, onProgress: progressHandler, onError: errorHandler});
			queue = new XMLLoader("data.xml", varsMax);
			//start loading
			queue.load();
			
			loaderComp = new LoaderComponent();
			containerSWF.addChild(loaderComp);
			loaderComp.scaleLoader = 0;
			
			ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, ldrComp);
			ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLdrProgress);
			ldr.contentLoaderInfo.addEventListener(Event.UNLOAD, onUnload);
			
			ldr2.contentLoaderInfo.addEventListener(Event.COMPLETE, ldrComp2);
			//ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLdrProgress2);
			ldr2.contentLoaderInfo.addEventListener(Event.UNLOAD, onUnload2);
			
		
			
			function completeHandler(event:LoaderEvent):void
			{
				//var image:ContentDisplay = LoaderMax.getContent("child");
				//containerSWF.addChild(image)
				//TweenLite.to(image, 1, {alpha: 1, y: 100});
				//trace(queue.content)
				var xml:XML = new XML(queue.content);
				//trace(xml.LoaderMax.@name);
				for each (var item:XML in xml.LoaderMax)
				{
					//trace(item.@name);
					//trace(item.elements('SWFLoader').@name);
					var element:Object = new Object();
					element.queue = item.@name.toString();
					element.name = item.elements('SWFLoader').@name.toString();
					loaders.push(element.queue);
						//loaderCount++;
				}
				//trace(event.target + " is complete!");
				//startLoading(loaders[0]);
				trace(loaders.length);
			}
			
			function errorHandler(event:LoaderEvent):void
			{
				trace("error occured with " + event.target + ": " + event.text);
			}
		}
		
		
		
		private function progressHandler(event:LoaderEvent):void
		{
			//trace("progress: " + event.target.progress);
			var kbps:Number = (queue.bytesLoaded / queue.loadTime) / 1024;
			var secondsLeft:Number = ((1 / queue.progress) - 1) * queue.loadTime;
			trace(kbps, secondsLeft);
			percent = event.target.progress;
			loaderComp.scaleLoader = percent;
		}
		
		private function startLoading(name:String):void
		{
			
			
			queue1 = LoaderMax.getLoader(name);
			
			
			if (queue1.hasEventListener(LoaderEvent.COMPLETE)) 
			queue1.removeEventListener(LoaderEvent.COMPLETE,onq1Comp);
			
			if (queue1.hasEventListener(LoaderEvent.CHILD_COMPLETE)) 
			queue1.removeEventListener(LoaderEvent.CHILD_COMPLETE, childComplete);
			
			if (queue1.hasEventListener(LoaderEvent.PROGRESS)) 
			queue1.removeEventListener(LoaderEvent.PROGRESS, progressHandler);
			
			queue1.addEventListener(LoaderEvent.COMPLETE, onq1Comp);
			queue1.addEventListener(LoaderEvent.CHILD_COMPLETE, childComplete);
			queue1.addEventListener(LoaderEvent.PROGRESS, progressHandler);
			//queue1.maxConnections = 1;
			queue1.prioritize();
			//queue1.vars = {format:"binary", onProgress: progressHandler};
			queue1.load();
		
		}
		
		private function childComplete(e:LoaderEvent):void
		{
			trace("child complete");
			trace(e.target);
			//var bt:ByteArray = LoaderMax.getContent(e.target.name);
			swfs.push(e.target.content as ByteArray);
			//e.target.addEventListener(LoaderEvent.COMPLETE, onCurrentComplete);
			//swfs.push(e.target.content);
		}
		
		private function onq1Comp(e:LoaderEvent):void
		{
			queue1.removeEventListener(LoaderEvent.COMPLETE, onq1Comp);
			queue1.removeEventListener(LoaderEvent.CHILD_COMPLETE, childComplete);
			queue1.removeEventListener(LoaderEvent.PROGRESS, progressHandler);
			//var loadedSWF:ContentDisplay = LoaderMax.getContent(loaders[2].name);
			//var loadedSWF:ContentDisplay = LoaderMax.getContent("child4");
			//var loadedSWF:SWFLoader = LoaderMax.getLoader("child1");
			//currentLoader = LoaderMax.getLoader("child1");
			//trace(currentLoader);
			//containerSWF.addChild(loadedSWF.content as ContentDisplay);
			//e.currentTarget.unload();
			//trace("numChildren: " + loadedSWF);
			//currentLoader = loadedSWF;
			//currentLoader = LoaderMax.getLoader("child1");
			
			//currentLoader = new SWFLoader(LoaderMax.getLoader("child1").url);
			//currentLoader.addEventListener(LoaderEvent.COMPLETE, onCurrentComplete);
			//setInterval(next, 2000);
			//setInterval(next, 3000);
			//setInterval(prev, 4000);
			
			//var bt:ByteArray = LoaderMax.getContent("child9");
			//trace(bt.length)
			//ldr = new Loader();
			//var ldrInfo:LoaderInfo = new LoaderInfo();
			//ldr.loadBytes(swfs[6] as ByteArray);
			
			//containerSWF.removeChild(ldr)
			trace("ALL DONE");
			trace(loaders)
			showNext();
			//if (loaderCount < loaders.length-1) {
			//startLoading(loaders[++loaderCount]);
			//trace(loaders[++loaderCount])
			//}
		
			//currentLoader.load();
		}
		
		private function ldrComp(e:Event):void
		{
			ldr2.unloadAndStop();
			
			
		
			var info:LoaderInfo = e.currentTarget as LoaderInfo;
			
			trace("LDR 1", info.content);
			containerSWF.removeChildren();
			containerSWF.addChild(ldr);
			containerSWF.addChild(loaderComp);
			trace("CONTENT 2    !!!!!", ldr.numChildren)
			//ldr.unloadAndStop();
			//containerSWF.removeChildren();
		
		}
		private function onUnload2(e:Event):void 
		{
			
		}
		private function onUnload(e:Event = null):void
		{
			//count > swfs.length - 1 ? count = 0 : count;
			//ldr.unloadAndStop();
			//containerSWF.removeChildren();
			//ldr.loadBytes(swfs[count++]);
			//trace("UNLOAD   ", " Num Children  ", containerSWF.numChildren);
		
		}
		private function ldrComp2(e:Event):void 
		{
			trace("LDR 2");
			ldr.unloadAndStop();
		
			
			
			var info:LoaderInfo = e.currentTarget as LoaderInfo;
			trace(info.content)
			containerSWF.removeChildren();
			//trace(info);
			containerSWF.addChild(ldr2);
			containerSWF.addChild(loaderComp);
		}
		
		private function onLdrProgress(e:ProgressEvent):void 
		{
			
				close = true;
		
		}
		
		private var count:int = 0;
		private var allowChange:Boolean = true;
		private var ldr:Loader = new Loader();
		private var loaderComp:LoaderComponent;
		private var close:Boolean = false;
		public var percent:Number = 0;
		
		private function showNext():void
		{
			//containerSWF.removeChildren();
			//if (ldr.content) {
			//trace(ldr.content, "  CONTENT");
			//ldr.unloadAndStop();
			//} else {
			//onUnload();
			//}
			
			//ldr.unload();
			count > swfs.length - 1 ? count = 0 : count;
			//close = true;
			//ldr.unloadAndStop();
			//var numCh:int = containerSWF.numChildren -1;
			//while (numCh>=0) 
			//{
				//containerSWF.removeChildAt(numCh--)
			//}
			
			//containerSWF.removeChildren();
			if (ldr.numChildren) 
			{
				ldr2.loadBytes(swfs[count++]);
				trace("CONTENT 1    !!!!!", ldr2.numChildren)
			} else {
				ldr.loadBytes(swfs[count++]);
				
			}
			
			//close = false;
		
		}
		private function showPrev():void
		{
			//containerSWF.removeChildren();
			//if (ldr.content) {
			//trace(ldr.content, "  CONTENT");
			//ldr.unloadAndStop();
			//} else {
			//onUnload();
			//}
			
			//ldr.unload();
			count < 0 ? count = swfs.length - 1 : count;
			//close = true;
			//ldr.unloadAndStop();
			//var numCh:int = containerSWF.numChildren -1;
			//while (numCh>=0) 
			//{
				//containerSWF.removeChildAt(numCh--)
			//}
			
			//containerSWF.removeChildren();
			if (ldr.numChildren) 
			{
				ldr2.loadBytes(swfs[count++]);
				trace("CONTENT 1    !!!!!", ldr2.numChildren)
			} else {
				ldr.loadBytes(swfs[count--]);
				
			}
			
			//close = false;
		
		}
		
		
		
		public function next():void
		{
			if (close == true) 
			{
				trace("AAAAAAAAAAAAAAAAAAAAAA");
				//ldr.unloadAndStop();
				close = false;
				
			}
			if (loaderCount < loaders.length)
			{
				//trace(loaders[loaderCount]);
				if (queue1)
				{
					//queue1.cancel();
					//trace(queue1.paused)
				}
				startLoading(loaders[loaderCount++]);
				
			}
			else
			{
				showNext();
			}
			
		
		}
		
		public function prev():void
		{
			if (loaderCount < loaders.length)
			{
				//trace(loaders[loaderCount]);
				if (queue1)
				{
					//queue1.cancel();
					//trace(queue1.paused)
				}
				startLoading(loaders[loaderCount++]);
				
			}
			else
			{
				showPrev();
			}
		}
		
		private function onCurrentComplete(e:LoaderEvent):void
		{
			
			//currentLoader.removeEventListener(LoaderEvent.COMPLETE, onCurrentComplete);
			//containerSWF.addChild(currentLoader.content as ContentDisplay);
			//containerSWF.addChild(currentLoader.getSWFChild(currentLoader.name));
			
			trace("currentComplete " + currentLoader + "\n");
			//allowChange = true;
			//trace(allowChange)
		}
		
		private function onCurrentPrevComplete(e:LoaderEvent):void
		{
			
			currentLoader.removeEventListener(LoaderEvent.COMPLETE, onCurrentPrevComplete);
			containerSWF.addChild(currentLoader.content as ContentDisplay);
			//containerSWF.addChild(currentLoader.getSWFChild(currentLoader.name));
			trace("currentComplete " + currentLoader + "\n");
			allowChange = true;
			trace(allowChange)
		}
		
		private function vars():void
		{
			var vars:SWFLoaderVars = new SWFLoaderVars();
			var varsMax:LoaderMaxVars = new LoaderMaxVars();
		
			//varsMax.loaders()
		}
	
	}

}